﻿using AutoMapper;
using Common.Property;
using DapperExtensions;
using DapperExtensions.Predicate;
using Domain.Draw;
using DTO;
using DTO.DTO.Draw.Entry;
using IRepository.Draw;
using IServices.Draw;

namespace Services.Draw
{
    public class EntryService : IEntryService
    {
        private IMapper _mapper;
        private IEntryRepository _repository;
        public EntryService(IMapper mapper, IEntryRepository repository)
        {
            _mapper = mapper;
            _repository = repository;
        }

        public async Task<bool> Delete(string id, OperatorInfo op)
        {
            return await _repository.DeleteAsync(id, op);
        }

        public async Task<bool> DeleteBatch(List<string> ids, OperatorInfo op)
        {
            return await _repository.DeleteBatchAsync(ids, op);
        }

        public async Task<Entry> GetById(string id)
        {
            return await _repository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<Entry>> GetByIds(List<string> ids)
        {
            return await _repository.GetByIdsAsync(ids);
        }

        public async Task<Tuple<IEnumerable<Entry>, int>> GetPageList(EntryQueryDTO dto)
        {
            var pgb = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
            if (!string.IsNullOrWhiteSpace(dto.Type))
            {
                pgb.Predicates.Add(Predicates.Field<Entry>(f => f.Type, Operator.Eq, dto.Type));
            }
            if (!string.IsNullOrWhiteSpace(dto.Type2))
            {
                pgb.Predicates.Add(Predicates.Field<Entry>(f => f.Type2, Operator.Eq, dto.Type2));
            }
            if (!string.IsNullOrWhiteSpace(dto.Type3))
            {
                pgb.Predicates.Add(Predicates.Field<Entry>(f => f.Type3, Operator.Eq, dto.Type3));
            }
            if (!string.IsNullOrWhiteSpace(dto.Keywords))
            {
                pgb.Predicates.Add(Predicates.Field<Entry>(f => f.Title, Operator.Like, $"%{dto.Keywords}%"));
            }
            if (!string.IsNullOrWhiteSpace(dto.Status))
            {
                pgb.Predicates.Add(Predicates.Field<Entry>(f => f.Status, Operator.Eq, dto.Status));
            }

            var result = await _repository.GetPageListAsync(dto.PageIndex, dto.PageSize, pgb);
            return result;
        }

        public async Task<IEnumerable<Entry>> GetList(EntryQueryDTO dto)
        {
            var pgb = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
            if (!string.IsNullOrWhiteSpace(dto.Type))
            {
                pgb.Predicates.Add(Predicates.Field<Entry>(f => f.Type, Operator.Eq, dto.Type));
            }
            if (!string.IsNullOrWhiteSpace(dto.Type2))
            {
                pgb.Predicates.Add(Predicates.Field<Entry>(f => f.Type2, Operator.Eq, dto.Type2));
            }
            if (!string.IsNullOrWhiteSpace(dto.Type3))
            {
                pgb.Predicates.Add(Predicates.Field<Entry>(f => f.Type3, Operator.Eq, dto.Type3));
            }
            if (!string.IsNullOrWhiteSpace(dto.Keywords))
            {
                pgb.Predicates.Add(Predicates.Field<Entry>(f => f.Title, Operator.Like, $"%{dto.Keywords}%"));
            }
            pgb.Predicates.Add(Predicates.Field<Entry>(f => f.Status, Operator.Eq, E_Status.Enabled.ToString()));

            var result = await _repository.GetListAsync(pgb);
            return result;
        }

        public async Task<Result> Save(EntryDetailDTO dto, OperatorInfo op)
        {
            if (string.IsNullOrWhiteSpace(dto.Id))
            {
                var model = _mapper.Map<Entry>(dto);
                var status = await _repository.InsertAsync(model, op);

                return status ? Result.Success("保存成功", model.Id) : Result.Failed(-1, "保存失败");
            }
            else
            {
                var model = await _repository.GetByIdAsync(dto.Id);
                _mapper.Map(dto, model);
                var status = await _repository.UpdateAsync(model, op);
                return status ? Result.Success("保存成功", model.Id) : Result.Failed(-1, "保存失败");
            }
        }
    }
}
